package com.hanhai.material.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysBadRecord;
import com.hanhai.material.device.entity.SysBadRecord1;
import com.hanhai.material.device.entity.SysProductRecord;
import com.hanhai.material.device.service.*;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.hanhai.material.device.entity.table.SysBadRecord1TableDef.SYS_BAD_RECORD1;
import static com.mybatisflex.core.query.QueryMethods.sum;


/**
 * 控制层。
 *
 * @author youchuang
 * @since 2024/04/28 21:16:19
 */
@RestController
@Api("接口")
@RequestMapping("/bad_record1")
@Log4j2
public class SysBadRecord1Controller {

    @Autowired
    private SysBadRecord1Service sysBadRecord1Service;
    @Autowired
    private SysBadRecordService sysBadRecordService;
    @Autowired
    private SysProductRecordService sysProductRecordService;
    @Autowired
    private SysTimeService sysTimeService;
    @Autowired
    private SysReasonType1Service sysReasonType1Service;
    @Autowired
    private SysInputNumberService sysInputNumberService;
    @Autowired
    private SysPlcService sysPlcService;

    /**
     * 添加。
     *
     * @param sysBadRecord1
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("/save")
    public Result save(@RequestBody SysBadRecord1 sysBadRecord1) {
//        System.out.println("Sys:{}",sysBadRecord1);
        boolean result = this.sysBadRecord1Service.save(sysBadRecord1);
        if(result&& "成型车间".equals(sysBadRecord1.getWorkShopName())){
            SysBadRecord sysBadRecord=JSONObject.parseObject(JSONObject.toJSONString(sysBadRecord1),SysBadRecord.class);
//            SysBadRecord sysBadRecord=new SysBadRecord();
            sysBadRecord.setId(UUID.randomUUID().toString());
            sysBadRecord.setArea(sysBadRecord1.getReasonType());
            sysBadRecord.setReasonType(sysBadRecord1.getReasonType1());
            sysBadRecord.setReasonName(sysBadRecord1.getReasonName());
            sysBadRecord.setShift(sysBadRecord1.getShift());
            sysBadRecord.setBadRecord1Id(String.valueOf(sysBadRecord1.getId()));
            log.error("SysBadRecord:{}",JSONObject.toJSONString(sysBadRecord));
            this.sysBadRecordService.save(sysBadRecord);
        }
        return result ? ResultTool.success() : ResultTool.fail();
    }

    @PostMapping("/count/{shift}/{productDate}")
    public Result<?> count(@RequestBody SysProductRecord sysProductRecord, @PathVariable String shift, @PathVariable Date productDate) {

        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
        queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"));
        queryWrapper.where(SYS_BAD_RECORD1.REASON_NAME.ne("极品"));
        SysBadRecord1 sysBadRecord = this.sysBadRecord1Service.getOne(queryWrapper);
        System.out.println("SysBad:" + sysBadRecord);
        if (sysBadRecord != null) {
            sysProductRecord.setBadNumPeople(sysBadRecord.getNum());
            if (sysProductRecord.getTotalNumPeople() > 0) {
                sysProductRecord.setGoodNumPeople(sysProductRecord.getTotalNumPeople() - sysProductRecord.getBadNumPeople());
            } else {
                sysProductRecord.setGoodNumPeople(0);
            }
        }
        this.sysProductRecordService.updateById(sysProductRecord);


        return sysBadRecord == null ? ResultTool.fail(sysProductRecord) : ResultTool.success(sysProductRecord);
    }
    @PostMapping("/count1/{shift}/{productDate}")
    public Result<?> count1(@RequestBody SysProductRecord sysProductRecord, @PathVariable String shift, @PathVariable Date productDate) {

        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
        queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"));
        queryWrapper.where(SYS_BAD_RECORD1.REASON_NAME.ne("极品"));
        SysBadRecord1 sysBadRecord = this.sysBadRecord1Service.getOne(queryWrapper);
        System.out.println("SysBad:" + sysBadRecord);
        if (sysBadRecord != null) {
            sysProductRecord.setBadNum(sysBadRecord.getNum());
            if (sysProductRecord.getTotalNumPeople() > 0) {
                sysProductRecord.setGoodNum(sysProductRecord.getTotalCount() - sysProductRecord.getBadNum());
            } else {
                sysProductRecord.setGoodNum(0);
            }
        }
        this.sysProductRecordService.updateById(sysProductRecord);


        return sysBadRecord == null ? ResultTool.fail(sysProductRecord) : ResultTool.success(sysProductRecord);
    }


    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return sysBadRecord1Service.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param sysBadRecord1
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") SysBadRecord1 sysBadRecord1) {
        return sysBadRecord1Service.updateById(sysBadRecord1);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<SysBadRecord1> list() {
        return sysBadRecord1Service.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public SysBadRecord1 getInfo(@PathVariable @ApiParam("主键") Serializable id) {
        return sysBadRecord1Service.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<SysBadRecord1> page(@ApiParam("分页信息") Page<SysBadRecord1> page) {
        return sysBadRecord1Service.page(page);
    }

//    @PostMapping("/getNine")
//    public Result<?> getNine(@RequestBody SysProductRecord sysProductRecord) throws ParseException {
//        BadNineModel1 badNineModel = new BadNineModel1();
//        QueryWrapper queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_TIME.IS_CONTAIN.eq(true));
//        List<SysTime> sysTimeList = this.sysTimeService.list(queryWrapper);
//        List<SysBadRecordNine1> allBadRecordNineList = new ArrayList<>();
//        queryWrapper = QueryWrapper.create();
//        queryWrapper.select(distinct(SYS_REASON_TYPE1.REASON_NAME).as("reason_name"));
//        queryWrapper.where(SYS_REASON_TYPE1.REASON_NAME.isNotNull());
//        queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//        List<SysReasonType1> reasonList = this.sysReasonType1Service.list(queryWrapper);
//        String productDate = fromDateToString("yyyy-MM-dd", sysProductRecord.getProductDate());
//
//        for (SysReasonType1 reason : reasonList) {
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_BAD_RECORD1.REASON_NAME.eq(reason.getReasonName()));
//            queryWrapper.where(SYS_BAD_RECORD1.WORK_PLAN_ID.eq(sysProductRecord.getWorkPlanId()));
//            queryWrapper.where(SYS_BAD_RECORD1.SHIFT.eq(sysProductRecord.getShift()));
//            queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd", sysProductRecord.getProductDate())));
//            queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"));
//            SysBadRecord1 badRecord = this.sysBadRecord1Service.getOne(queryWrapper);
//            log.error("SysBadSQL:{}", queryWrapper.toSQL());
//            if (badRecord != null) {
//                reason.setNum(badRecord.getNum());
//            }
//        }
//
//        for (SysTime sysTime : sysTimeList) {
//
//            List<SysBadRecord1> allBadRecordList = new ArrayList<>();
//            SysBadRecordNine1 badRecordNine = new SysBadRecordNine1();
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_REASON_TYPE1.PID.eq(0));
//            queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//            List<SysReasonType1> reasonTypeList1 = this.sysReasonType1Service.list(queryWrapper);
//
//
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_INPUT_NUMBER.SHIFT.eq(sysProductRecord.getShift()));
//            queryWrapper.where(SYS_INPUT_NUMBER.WORK_PLAN_ID.eq(sysProductRecord.getWorkPlanId()));
//            queryWrapper.where(SYS_INPUT_NUMBER.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysProductRecord.getProductDate())));
//            if (sysProductRecord.getShift().equals("白班")) {
//                queryWrapper.where(SYS_INPUT_NUMBER.TIME_PERIOD.eq(sysTime.getLightTime()));
//            } else {
//                queryWrapper.where(SYS_INPUT_NUMBER.TIME_PERIOD.eq(sysTime.getNightTime()));
//            }
//            queryWrapper.select(sum(SYS_INPUT_NUMBER.NUM).as("num"));
//            SysInputNumber sysInputNumber = this.sysInputNumberService.getOne(queryWrapper);
//            for (SysReasonType1 reasonType1 : reasonTypeList1) {
//                queryWrapper = QueryWrapper.create();
//                queryWrapper.where(SYS_REASON_TYPE1.PID.eq(reasonType1.getId()));
//                queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//                List<SysReasonType1> reasonTypeList = this.sysReasonType1Service.list(queryWrapper);
//
//                SysBadRecordNine1 sysBadRecordNine1 = new SysBadRecordNine1();
//
//                sysBadRecordNine1.setReasonType1(reasonType1.getReasonTypeName());
//                sysBadRecordNine1.setReasonType("小计");
//                sysBadRecordNine1.setBadRecordList(allBadRecordList);
//                if (sysProductRecord.getShift().equals("白班")) {
//                    sysBadRecordNine1.setLabel(sysTime.getLightTime());
//
//                } else {
//                    sysBadRecordNine1.setLabel(sysTime.getNightTime());
//
//                }
//
//                if (sysInputNumber != null) {
//                    sysBadRecordNine1.setInputNumber(sysInputNumber.getNum());
//
//                }
//                Date startTime = new Date();
//                Date endTime = new Date();
//                if (sysProductRecord.getShift().equals("白班")) {
//
//                    log.error("白板班别");
//                    String startS = sysTime.getLightTime().split("-")[0] + ":00";
//                    String endS = sysTime.getLightTime().split("-")[1] + ":00";
//                    startTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                    endTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//
//                }
//                else {
//                    log.error("黑白板班别");
//                    String startS = sysTime.getNightTime().split("-")[0] + ":00";
//                    startTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                    String endS = sysTime.getNightTime().split("-")[1] + ":00";
//                    ;
//                    if (sysTime.getTimeIndex() == 10) {
//                        endS = "23:59:59";
//                    }
//                    if (sysTime.getTimeIndex() > 10) {
//                        LocalDate date = sysProductRecord.getProductDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusDays(1);
//                        productDate = fromDateToString("yyyy-MM-dd", Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
//                    }
//
//                    endTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//                }
//
//                queryWrapper = QueryWrapper.create();
//                queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"), SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                queryWrapper.where(SYS_BAD_RECORD1.CREATE_TIME.between(startTime, endTime));
//                queryWrapper.where(SYS_BAD_RECORD1.WORK_PLAN_ID.eq(sysProductRecord.getWorkPlanId()));
//                queryWrapper.where(SYS_BAD_RECORD1.SHIFT.eq(sysProductRecord.getShift()));
//                queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd", sysProductRecord.getProductDate())));
//                queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID.eq(reasonType1.getId()));
//                queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE1.eq("极品"));
//                queryWrapper.groupBy(SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                List<SysBadRecord1> adRecordList = this.sysBadRecord1Service.list(queryWrapper);
//                log.error("Quer:{}",queryWrapper.toSQL());
//                int premium = adRecordList.stream().mapToInt(SysBadRecord1::getNum).sum();
//                allBadRecordNineList.add(sysBadRecordNine1);
//                sysBadRecordNine1.setPremiumNumber(premium);
//
//                for (SysReasonType1 reasonType : reasonTypeList) {
//                    SysBadRecordNine1 sysBadRecordNine = new SysBadRecordNine1();
//                    Date startTime1= new Date();
//                    Date endTime1 = new Date();
//                    if (sysProductRecord.getShift().equals("白班")) {
//                        log.error("白板班别");
//                        sysBadRecordNine.setLabel(sysTime.getLightTime());
//                        badRecordNine.setLabel(sysTime.getLightTime());
//                        String startS = sysTime.getLightTime().split("-")[0] + ":00";
//                        String endS = sysTime.getLightTime().split("-")[1] + ":00";
//                        startTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                        endTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//
//                    } else {
//                        log.error("黑白板班别");
//
//                        sysBadRecordNine.setLabel(sysTime.getNightTime());
//                        badRecordNine.setLabel(sysTime.getNightTime());
//                        String startS = sysTime.getNightTime().split("-")[0] + ":00";
//                        startTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                        String endS = sysTime.getNightTime().split("-")[1] + ":00";
//                        ;
//                        if (sysTime.getTimeIndex() == 10) {
//                            endS = "23:59:59";
//                        }
//                        if (sysTime.getTimeIndex() > 10) {
//                            LocalDate date = sysProductRecord.getProductDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusDays(1);
//                            productDate = fromDateToString("yyyy-MM-dd", Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
//                        }
//
//                        endTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//                    }
//                    queryWrapper = QueryWrapper.create();
//                    queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"), SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                    queryWrapper.where(SYS_BAD_RECORD1.CREATE_TIME.between(startTime1, endTime1));
//                    queryWrapper.where(SYS_BAD_RECORD1.WORK_PLAN_ID.eq(sysProductRecord.getWorkPlanId()));
//                    queryWrapper.where(SYS_BAD_RECORD1.SHIFT.eq(sysProductRecord.getShift()));
//                    queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd", sysProductRecord.getProductDate())));
//                    queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID.eq(reasonType1.getId()));
//                    queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID1.eq(reasonType.getId()));
//                    queryWrapper.groupBy(SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                    List<SysBadRecord1> sysBadRecordList = this.sysBadRecord1Service.list(queryWrapper);
//                    if (!Objects.equals(reasonType.getReasonTypeName(), "极品")) {
//                        if (sysBadRecordList.size() > 0 && sysBadRecordList.get(0) != null) {
//                            sysBadRecordNine.setBadRecordList(sysBadRecordList);
//                            allBadRecordList.addAll(sysBadRecordList);
//                        } else {
//                            sysBadRecordNine.setBadRecordNineList(null);
//                        }
//                        sysBadRecordNine.setReasonType1(reasonType1.getReasonTypeName());
//                        sysBadRecordNine.setReasonType(reasonType.getReasonTypeName());
//                        allBadRecordNineList.add(sysBadRecordNine);
//
//                    } else {
//
//                    }
//
//                }
//
//            }
//
//        }
//        badNineModel.setBadRecordNineList(allBadRecordNineList);
//        badNineModel.setReasonList(reasonList);
//        return allBadRecordNineList.isEmpty() ? ResultTool.fail() : ResultTool.success(badNineModel);
//    }
//
//    @PostMapping("/getNine1")
//    public Result<?> getNine1(@RequestBody SysProductRecord sysProductRecord) throws ParseException {
//        BadNineModel1 badNineModel = new BadNineModel1();
//        QueryWrapper queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_TIME.IS_CONTAIN.eq(true));
//        List<SysTime> sysTimeList = this.sysTimeService.list(queryWrapper);
//        List<SysBadRecordNine1> allBadRecordNineList = new ArrayList<>();
//        queryWrapper = QueryWrapper.create();
//        queryWrapper.select(distinct(SYS_REASON_TYPE1.REASON_NAME).as("reason_name"));
//        queryWrapper.where(SYS_REASON_TYPE1.REASON_NAME.isNotNull());
//        queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//        List<SysReasonType1> reasonList = this.sysReasonType1Service.list(queryWrapper);
//        String productDate = fromDateToString("yyyy-MM-dd", sysProductRecord.getProductDate());
//
//        for (SysReasonType1 reason : reasonList) {
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_BAD_RECORD1.REASON_NAME.eq(reason.getReasonName()));
//            queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
//            queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"));
//            SysBadRecord1 badRecord = this.sysBadRecord1Service.getOne(queryWrapper);
//            log.error("SysBadSQL:{}", queryWrapper.toSQL());
//            if (badRecord != null) {
//                reason.setNum(badRecord.getNum());
//            }
//        }
//
//        for (SysTime sysTime : sysTimeList) {
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_INPUT_NUMBER.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
//            if (sysProductRecord.getShift().equals("白班")) {
//                queryWrapper.where(SYS_INPUT_NUMBER.TIME_PERIOD.eq(sysTime.getLightTime()));
//            } else {
//                queryWrapper.where(SYS_INPUT_NUMBER.TIME_PERIOD.eq(sysTime.getNightTime()));
//            }
//            SysInputNumber sysInputNumber = this.sysInputNumberService.getOne(queryWrapper);
//            log.error("SYSSQLSS:{}", queryWrapper.toSQL());
//            List<SysBadRecord1> allBadRecordList = new ArrayList<>();
//            SysBadRecordNine1 badRecordNine = new SysBadRecordNine1();
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_REASON_TYPE1.PID.eq(0));
//            queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//            List<SysReasonType1> reasonTypeList1 = this.sysReasonType1Service.list(queryWrapper);
//            log.error("ReasonTypeList1:{}", JSONObject.toJSONString(reasonTypeList1));
//            for (SysReasonType1 reasonType1 : reasonTypeList1) {
//                queryWrapper = QueryWrapper.create();
//                queryWrapper.where(SYS_REASON_TYPE1.PID.eq(reasonType1.getId()));
//                queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//                List<SysReasonType1> reasonTypeList = this.sysReasonType1Service.list(queryWrapper);
//
//                SysBadRecordNine1 sysBadRecordNine1 = new SysBadRecordNine1();
//                sysBadRecordNine1.setTimeIndex(sysTime.getTimeIndex());
//                sysBadRecordNine1.setReasonType1(reasonType1.getReasonTypeName());
//                sysBadRecordNine1.setReasonType("小计");
//
//
//                if (sysProductRecord.getShift().equals("白班")) {
//                    sysBadRecordNine1.setLabel(sysTime.getLightTime());
//
//                } else {
//                    sysBadRecordNine1.setLabel(sysTime.getNightTime());
//
//                }
//                if (sysInputNumber != null) {
//                    sysBadRecordNine1.setInputNumber(sysInputNumber.getNum());
//
//                }
//                Date startTime = new Date();
//                Date endTime = new Date();
//                if (sysProductRecord.getShift().equals("白班")) {
//
//                    log.error("白板班别");
//                    String startS = sysTime.getLightTime().split("-")[0] + ":00";
//                    String endS = sysTime.getLightTime().split("-")[1] + ":00";
//                    startTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                    endTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//
//                }
//                else {
//                    log.error("黑白板班别");
//                    String startS = sysTime.getNightTime().split("-")[0] + ":00";
//                    startTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                    String endS = sysTime.getNightTime().split("-")[1] + ":00";
//                    ;
//                    if (sysTime.getTimeIndex() == 10) {
//                        endS = "23:59:59";
//                    }
//                    if (sysTime.getTimeIndex() > 10) {
//                        LocalDate date = sysProductRecord.getProductDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusDays(1);
//                        productDate = fromDateToString("yyyy-MM-dd", Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
//                    }
//
//                    endTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//                }
//
//                queryWrapper = QueryWrapper.create();
//                queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"), SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                queryWrapper.where(SYS_BAD_RECORD1.CREATE_TIME.between(startTime, endTime));
//                queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
//                queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID.eq(reasonType1.getId()));
//                queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE1.eq("极品"));
//                queryWrapper.groupBy(SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                List<SysBadRecord1> adRecordList = this.sysBadRecord1Service.list(queryWrapper);
//                log.error("Quer:{}",queryWrapper.toSQL());
//                int premium = adRecordList.stream().mapToInt(SysBadRecord1::getNum).sum();
//                allBadRecordNineList.add(sysBadRecordNine1);
//                sysBadRecordNine1.setPremiumNumber(premium);
//
//                sysBadRecordNine1.setBadRecordList(allBadRecordList);
//                for (SysReasonType1 reasonType : reasonTypeList) {
//                    SysBadRecordNine1 sysBadRecordNine = new SysBadRecordNine1();
//                    Date startTime1 = new Date();
//                    Date endTime1 = new Date();
//                    if (sysProductRecord.getShift().equals("白班")) {
//                        if (sysInputNumber != null) {
//                            sysBadRecordNine.setInputNumber(sysInputNumber.getNum());
//                        }
//                        log.error("白板班别");
//                        sysBadRecordNine.setLabel(sysTime.getLightTime());
//                        badRecordNine.setLabel(sysTime.getLightTime());
//                        String startS = sysTime.getLightTime().split("-")[0] + ":00";
//                        String endS = sysTime.getLightTime().split("-")[1] + ":00";
//                        startTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                        endTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//
//                    } else {
//                        log.error("黑白板班别");
//                        if (sysInputNumber != null) {
//                            sysBadRecordNine.setInputNumber(sysInputNumber.getNum());
//                        }
//                        sysBadRecordNine.setLabel(sysTime.getNightTime());
//                        sysBadRecordNine.setTimeIndex(sysTime.getTimeIndex());
//                        badRecordNine.setLabel(sysTime.getNightTime());
//                        String startS = sysTime.getNightTime().split("-")[0] + ":00";
//                        startTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                        String endS = sysTime.getNightTime().split("-")[1] + ":00";
//                        ;
//                        if (sysTime.getTimeIndex() == 10) {
//                            endS = "23:59:59";
//                        }
//                        if (sysTime.getTimeIndex() > 10) {
//                            LocalDate date = sysProductRecord.getProductDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusDays(1);
//                            productDate = fromDateToString("yyyy-MM-dd", Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
//                        }
//
//                        endTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//                    }
//
//                    queryWrapper = QueryWrapper.create();
//                    queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"), SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                    queryWrapper.where(SYS_BAD_RECORD1.CREATE_TIME.between(startTime1, endTime1));
//                    queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
//                    queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID.eq(reasonType1.getId()));
//                    queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID1.eq(reasonType.getId()));
//                    queryWrapper.groupBy(SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                    List<SysBadRecord1> sysBadRecordList = this.sysBadRecord1Service.list(queryWrapper);
//                    log.error("SQLSS:{}", queryWrapper.toSQL());
//                    if (!Objects.equals(reasonType.getReasonTypeName(), "极品")) {
//                        if (sysBadRecordList.size() > 0 && sysBadRecordList.get(0) != null) {
//                            sysBadRecordNine.setBadRecordList(sysBadRecordList);
//                            allBadRecordList.addAll(sysBadRecordList);
//                        } else {
//                            sysBadRecordNine.setBadRecordNineList(null);
//                        }
//                        sysBadRecordNine.setReasonType1(reasonType1.getReasonTypeName());
//                        sysBadRecordNine.setReasonType(reasonType.getReasonTypeName());
//                        sysBadRecordNine.setTimeIndex(sysTime.getTimeIndex());
//                        allBadRecordNineList.add(sysBadRecordNine);
//
//                    } else {
//
//                    }
//
//                }
//
//            }
//
//        }
//        badNineModel.setBadRecordNineList(allBadRecordNineList);
//        badNineModel.setReasonList(reasonList);
//        return allBadRecordNineList.isEmpty() ? ResultTool.fail() : ResultTool.success(badNineModel);
//    }
//    @PostMapping("/getNine2")
//    public Result<?> getNine2(@RequestBody SysProductRecord sysProductRecord) throws ParseException {
//        BadNineModel1 badNineModel = new BadNineModel1();
//        QueryWrapper queryWrapper = QueryWrapper.create();
//        queryWrapper.where(SYS_TIME.IS_CONTAIN.eq(true));
//        queryWrapper.where(SYS_TIME.TIME_INDEX.ne(20));
//        List<SysTime> sysTimeList = this.sysTimeService.list(queryWrapper);
//        List<SysBadRecordNine1> allBadRecordNineList = new ArrayList<>();
//        queryWrapper = QueryWrapper.create();
//        queryWrapper.select(distinct(SYS_REASON_TYPE1.REASON_NAME).as("reason_name"));
//        queryWrapper.where(SYS_REASON_TYPE1.REASON_NAME.isNotNull());
//        queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//        List<SysReasonType1> reasonList = this.sysReasonType1Service.list(queryWrapper);
//        String productDate = fromDateToString("yyyy-MM-dd", sysProductRecord.getProductDate());
//
//        for (SysReasonType1 reason : reasonList) {
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_BAD_RECORD1.REASON_NAME.eq(reason.getReasonName()));
//            queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
//            queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"));
//            SysBadRecord1 badRecord = this.sysBadRecord1Service.getOne(queryWrapper);
//            log.error("SysBadSQL:{}", queryWrapper.toSQL());
//            if (badRecord != null) {
//                reason.setNum(badRecord.getNum());
//            }
//        }
//
//        for (SysTime sysTime : sysTimeList) {
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_PLC.MACHINE_NAME.eq(sysProductRecord.getMachineName()));
//            if (sysProductRecord.getShift().equals("白班")) {
//                LocalDate date=LocalDate.now();
//                queryWrapper.where(SYS_PLC.CREATE_TIME.between(date.atTime(sysTime.getTimeIndex(),0,0),date.atTime(sysTime.getTimeIndex()+2,0,0)));
//            } else {
//                LocalDate date = LocalDate.now();
//                int time = 0;
//                if (sysTime.getTimeIndex() < 12) {
//                    time = sysTime.getTimeIndex() + 12;
//                } else if (sysTime.getTimeIndex() > 12) {
//                    time = sysTime.getTimeIndex() - 12;
//                } else {
//                    time = 0;
//                }
//                if (time<22) {
//                    queryWrapper.where(SYS_PLC.CREATE_TIME.between(date.atTime(time, 0, 0), date.atTime(time + 2, 0, 0)));
//
//                } else if (time == 22) {
//                    queryWrapper.where(SYS_PLC.CREATE_TIME.between(date.atTime(time, 0, 0), date.atTime(time + 1, 59, 59)));
//                }else{
//                    date=date.minusDays(1);
//                    queryWrapper.where(SYS_PLC.CREATE_TIME.between(date.atTime(time, 0, 0), date.atTime(time + 2, 0, 0)));
//
//                }
//
//
//            }
//            int num=0;
//            queryWrapper.select(max(SYS_PLC.MODE_NUM).subtract(min(SYS_PLC.MODE_NUM)).as("mode_num"))
//                    .select(SYS_PLC.ID, SYS_PLC.CREATE_TIME, SYS_PLC.MACHINE_NAME);
//            log.info("SQL:{}",queryWrapper.toSQL());
//            SysPlc sysPlc = this.sysPlcService.getOne(queryWrapper);
//            if(sysPlc!=null){
//
//                num = (int) (sysProductRecord.getWorkPlan().getMoldNumber()*sysPlc.getModeNum());
//
//
//            }
//
//            log.error("SYSSQLSS:{}", queryWrapper.toSQL());
//            List<SysBadRecord1> allBadRecordList = new ArrayList<>();
//            SysBadRecordNine1 badRecordNine = new SysBadRecordNine1();
//            queryWrapper = QueryWrapper.create();
//            queryWrapper.where(SYS_REASON_TYPE1.PID.eq(0));
//            queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//            List<SysReasonType1> reasonTypeList1 = this.sysReasonType1Service.list(queryWrapper);
//            log.error("ReasonTypeList1:{}", JSONObject.toJSONString(reasonTypeList1));
//            for (SysReasonType1 reasonType1 : reasonTypeList1) {
//                queryWrapper = QueryWrapper.create();
//                queryWrapper.where(SYS_REASON_TYPE1.PID.eq(reasonType1.getId()));
//                queryWrapper.where(SYS_REASON_TYPE1.WORK_SHOP.eq(sysProductRecord.getWorkShop()));
//                List<SysReasonType1> reasonTypeList = this.sysReasonType1Service.list(queryWrapper);
//
//                SysBadRecordNine1 sysBadRecordNine1 = new SysBadRecordNine1();
//                sysBadRecordNine1.setTimeIndex(sysTime.getTimeIndex());
//                sysBadRecordNine1.setReasonType1(reasonType1.getReasonTypeName());
//                sysBadRecordNine1.setReasonType("小计");
//
//
//                if (sysProductRecord.getShift().equals("白班")) {
//                    sysBadRecordNine1.setLabel(sysTime.getLightTime());
//
//                } else {
//                    sysBadRecordNine1.setLabel(sysTime.getNightTime());
//
//                }
//
//                sysBadRecordNine1.setInputNumber(num);
//
//
//                Date startTime = new Date();
//                Date endTime = new Date();
//                if (sysProductRecord.getShift().equals("白班")) {
//
//                    log.error("白板班别");
//                    String startS = sysTime.getLightTime().split("-")[0] + ":00";
//                    String endS = sysTime.getLightTime().split("-")[1] + ":00";
//                    startTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                    endTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//
//                }
//                else {
//                    log.error("黑白板班别");
//                    String startS = sysTime.getNightTime().split("-")[0] + ":00";
//                    startTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                    String endS = sysTime.getNightTime().split("-")[1] + ":00";
//                    ;
//                    if (sysTime.getTimeIndex() == 10) {
//                        endS = "23:59:59";
//                    }
//                    if (sysTime.getTimeIndex() > 10) {
//                        LocalDate date = sysProductRecord.getProductDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusDays(1);
//                        productDate = fromDateToString("yyyy-MM-dd", Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
//                    }
//
//                    endTime = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//                }
//
//                queryWrapper = QueryWrapper.create();
//                queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"), SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                queryWrapper.where(SYS_BAD_RECORD1.CREATE_TIME.between(startTime, endTime));
//                queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
//                queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID.eq(reasonType1.getId()));
//                queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE1.eq("极品"));
//                queryWrapper.groupBy(SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                List<SysBadRecord1> adRecordList = this.sysBadRecord1Service.list(queryWrapper);
//                log.error("Quer:{}",queryWrapper.toSQL());
//                int premium = adRecordList.stream().mapToInt(SysBadRecord1::getNum).sum();
//                allBadRecordNineList.add(sysBadRecordNine1);
//                sysBadRecordNine1.setPremiumNumber(premium);
//
//                sysBadRecordNine1.setBadRecordList(allBadRecordList);
//                for (SysReasonType1 reasonType : reasonTypeList) {
//                    SysBadRecordNine1 sysBadRecordNine = new SysBadRecordNine1();
//                    Date startTime1 = new Date();
//                    Date endTime1 = new Date();
//                    if (sysProductRecord.getShift().equals("白班")) {
//
//                        sysBadRecordNine.setInputNumber(num);
//
//                        log.error("白板班别");
//                        sysBadRecordNine.setLabel(sysTime.getLightTime());
//                        badRecordNine.setLabel(sysTime.getLightTime());
//                        String startS = sysTime.getLightTime().split("-")[0] + ":00";
//                        String endS = sysTime.getLightTime().split("-")[1] + ":00";
//                        startTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                        endTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//
//                    } else {
//                        log.error("黑白板班别");
//
//                        sysBadRecordNine.setInputNumber(num);
//
//                        sysBadRecordNine.setLabel(sysTime.getNightTime());
//                        sysBadRecordNine.setTimeIndex(sysTime.getTimeIndex());
//                        badRecordNine.setLabel(sysTime.getNightTime());
//                        String startS = sysTime.getNightTime().split("-")[0] + ":00";
//                        startTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + startS);
//                        String endS = sysTime.getNightTime().split("-")[1] + ":00";
//                        ;
//                        if (sysTime.getTimeIndex() == 10) {
//                            endS = "23:59:59";
//                        }
//                        if (sysTime.getTimeIndex() > 10) {
//                            LocalDate date = sysProductRecord.getProductDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().plusDays(1);
//                            productDate = fromDateToString("yyyy-MM-dd", Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()));
//                        }
//
//                        endTime1 = fromStringToDate("yyyy-MM-dd HH:mm:ss", productDate + " " + endS);
//                    }
//
//                    queryWrapper = QueryWrapper.create();
//                    queryWrapper.select(sum(SYS_BAD_RECORD1.NUM).as("num"), SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                    queryWrapper.where(SYS_BAD_RECORD1.CREATE_TIME.between(startTime1, endTime1));
//                    queryWrapper.where(SYS_BAD_RECORD1.PRODUCT_RECORD_ID.eq(sysProductRecord.getId()));
//                    queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID.eq(reasonType1.getId()));
//                    queryWrapper.where(SYS_BAD_RECORD1.REASON_TYPE_ID1.eq(reasonType.getId()));
//                    queryWrapper.groupBy(SYS_BAD_RECORD1.REASON_NAME, SYS_BAD_RECORD1.REASON_TYPE);
//                    List<SysBadRecord1> sysBadRecordList = this.sysBadRecord1Service.list(queryWrapper);
//                    log.error("SQLSS:{}", queryWrapper.toSQL());
//                    if (!Objects.equals(reasonType.getReasonTypeName(), "极品")) {
//                        if (sysBadRecordList.size() > 0 && sysBadRecordList.get(0) != null) {
//                            sysBadRecordNine.setBadRecordList(sysBadRecordList);
//                            allBadRecordList.addAll(sysBadRecordList);
//                        } else {
//                            sysBadRecordNine.setBadRecordNineList(null);
//                        }
//                        sysBadRecordNine.setReasonType1(reasonType1.getReasonTypeName());
//                        sysBadRecordNine.setReasonType(reasonType.getReasonTypeName());
//                        sysBadRecordNine.setTimeIndex(sysTime.getTimeIndex());
//                        allBadRecordNineList.add(sysBadRecordNine);
//
//                    } else {
//
//                    }
//
//                }
//
//            }
//
//        }
//        badNineModel.setBadRecordNineList(allBadRecordNineList);
//        badNineModel.setReasonList(reasonList);
//        return allBadRecordNineList.isEmpty() ? ResultTool.fail() : ResultTool.success(badNineModel);
//    }
}
